#include "watchDog_hw.h"
#include "gd32e10x_fwdgt.h"



extern uint32_t SystemCoreClock;

/*
看门狗导致的重启 = 1
否则 = 0
*/
uint8_t	watchDogRebootFlag = 0;


/*
初始化看门狗
6.4秒喂狗间隔
*/
uint8_t initWatchDog(void)
{
	uint32_t waitMaxTime = 0;
	
	/* enable IRC40K */
	rcu_osci_on(RCU_IRC40K);
	
	/* wait till IRC40K is ready */
	while(SUCCESS != rcu_osci_stab_wait(RCU_IRC40K))
	{
		waitMaxTime++;
		if (waitMaxTime > (SystemCoreClock / 20))
		{
			return 1;		//内部时钟无法起振导致的初始化失败,预防
		}
	}
	
	/* 6.4s */
	fwdgt_config(1000,FWDGT_PSC_DIV256);
	
	/* after 1.6 seconds to generate a reset */
	fwdgt_enable();
	
	/* check if the system has resumed from FWDGT reset */
	if (RESET != rcu_flag_get(RCU_FLAG_FWDGTRST))
	{
		/* clear the FWDGT reset flag */
		rcu_all_reset_flag_clear();
			
		watchDogRebootFlag = 1;
	}
	else
	{
		watchDogRebootFlag = 0;
	}
	
	return 0;

}

/*
喂狗
*/
void feedWatchDog(void)
{
	fwdgt_counter_reload();
}

/*
获取是否重启自看门狗

因看门狗导致的重启返回1
否则返回0
*/
uint8_t CheckWatchDogReboot(void)
{
	return watchDogRebootFlag;
}









